/*
 * CdnArea.cpp
 *
 *  Created on: Apr 22, 2013
 *      Author: root
 */

#include "CdnArea.h"
/*
 * class init
 */
CdnArea::CdnArea()
{
	initArea(AREA);
}
/*
 * class init
 */
CdnArea::CdnArea(AreaType _type)
{
	initArea(_type);
}
/**
 * class free
 */
CdnArea::~CdnArea()
{
	/**
	 * delete chid node
	 */

	CdnArea * freenode = NULL;
	CdnArea * optr = this->firstChild;
	while(optr != NULL)
	{
		freenode = optr;
		optr = optr->nextSibling;

		delete freenode;
		freenode = NULL;
	}

	this->childCount = 0;
	this->firstChild = NULL;
	this->lastChild = NULL;
	this->nextSibling = NULL;
	this->prevSibling = NULL;
}

/**
 * initArea
 */
void CdnArea::initArea(AreaType _type)
{
	id= 0;
	memset(this->name,'\0',sizeof(this->name));
	this->type = _type;

	nodeIndex = 0;
	childCount = 0;
	treelevel = 0;

	parent = NULL;
	firstChild = NULL;
	lastChild = NULL;
	nextSibling = NULL;
	prevSibling = NULL;
}

/**
 * Apend Child To Last
 */
int CdnArea::ApendChildToLast(CdnArea * pNode)
{
	if(pNode == NULL)
		return FAILURE;

	pNode->parent = this;
	pNode->lastChild    = pNode->firstChild  = NULL;
	pNode->nextSibling  = pNode->prevSibling = NULL;

	if (this->firstChild == NULL)
	{
		this->firstChild = pNode;
		this->lastChild = pNode;
	}
	else
	{
		this->lastChild->nextSibling = pNode;
		pNode->prevSibling = this->lastChild;
		this->lastChild = pNode;
	}

	/*set node index */
	pNode->nodeIndex = this->childCount;
	pNode->treelevel = this->treelevel + 1;
	this->childCount++;

	/*add success*/
	return SUCCESS;
}

int CdnArea::InitNode(int _id,const char * pname)
{
	this->id = _id;
	this->SetName(pname);

	/*call success*/
	return SUCCESS;
}

int CdnArea::GetId()
{
	return this->id;
}

void CdnArea::SetName(const char * pname)
{
	if(pname == NULL)
		return ;

	if(strlen(pname) < GUID_LEN)
	{
		memset(this->name,'\0',sizeof(this->name));
		strcpy(this->name,pname);
	}
}

const char * CdnArea::GetName()
{
	return this->name;
}

AreaType CdnArea::GetType()
{
	return this->type;
}

void CdnArea::lock()
{
	this->olock.Lock();
}

void CdnArea::unlock()
{
	this->olock.Unlock();
}
